package com.genexususa.eo.bing.data.wsSubclasses;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.AdRotation;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.AdRotationType;
import com.genexususa.eo.bing.data.GxItemException;
import com.genexususa.eo.bing.data.Util;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class GxAdRotation extends AdRotation {
    public GxAdRotation(Element element) throws IllegalArgumentException, GxItemException {
        String dateString = null;
        Util util = new Util();
        if (element.getElementsByTagName("EndDate").getLength() > 0 &&
            element.getElementsByTagName("EndDate").item(0).getChildNodes().getLength() > 0) {
            dateString = ((Node) element.getElementsByTagName("EndDate").item(0).getChildNodes().item(0)).getNodeValue();
            if (dateString == null || dateString.equals("")) {
                try {
                    this.setEndDate(util.calendarFromString(dateString));
                } catch (GxItemException e) {
                    throw new GxItemException("EndDate format is invalid");
                }
            }
        }
        if (element.getElementsByTagName("StartDate").getLength() > 0 &&
            element.getElementsByTagName("StartDate").item(0).getChildNodes().getLength() > 0) {
            dateString = ((Node) element.getElementsByTagName("StartDate").item(0).getChildNodes().item(0)).getNodeValue();
            if (dateString == null || dateString.equals("")) {
                try {
                    this.setStartDate(util.calendarFromString(dateString));
                } catch (GxItemException e) {
                    throw new GxItemException("StartDate format is invalid");
                }
            }
        }
        if (element.getElementsByTagName("Type").getLength() > 0 &&
            element.getElementsByTagName("Type").item(0).getChildNodes().getLength() > 0) {
            this.setType(AdRotationType.fromString(((Node) element.getElementsByTagName("Type").item(0).getChildNodes().item(0)).getNodeValue()));
        }
    }
}
